import { createSlice, type PayloadAction } from "@reduxjs/toolkit";

interface IFilm {
  id: string;
  name: string;
}

const filmSlice = createSlice({
  name: "film",

  initialState: {
    items: [
      { id: "1", name: "吃饭" },
      { id: "2", name: "睡觉" },
    ],
  },

  reducers: {
    addFilm(state, { payload }: PayloadAction<IFilm>) {
      state.items.push(payload);
    },

    delFilm(state, { payload: id }: PayloadAction<string>) {
      state.items = state.items.filter((item) => item.id !== id);
    },

    // ...
  },
});

export const { addFilm, delFilm } = filmSlice.actions;

// addFilm() => { type: 'film/addFilm', payload: undefined }
// delFilm() => { type: 'film/delFilm', payload: undefined }

export default filmSlice.reducer;
